import axios from 'axios'
import { Toast } from 'vant';
import router from '../router'
axios.defaults.withCredentials = true
const cookie = localStorage.getItem('shopwx')
axios.interceptors.response.use((res) =>{
    if(res.data.code != 200){
        Toast.fail(res.data.message);
        if(res.data.code==999||res.data.code==996){
            localStorage.removeItem("shopwx")
            router.push('/login')
        }
    }
    return res;
}, (error) => {
    return Promise.reject(error);
});
axios.interceptors.request.use(
    config => {
        const token = localStorage.getItem("shopwx");
        token && (config.headers.mlfastsessionid = token);
        return config;
    },
    error => {
        return Promise.error(error);
    }
);
export function fetchPost(url, params={}) {
    return axios({
        method:"POST",
        url:url,
        data:params,
    })
};
export function fetchGet(url, param) {
    return new Promise((resolve, reject) => {
        axios.get(url, {params: param})
            .then(response => {
                resolve(response)
            }, err => {
                reject(err)
            })
            .catch((error) => {
                reject(error)
            })
    })
}